package org.jsola.hr.web;

import org.jsola.core.Result;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.NoticeRuleSettingDTO;
import org.jsola.hr.service.INoticeService;
import org.jsola.hr.task.SendNoticeTask;
import org.jsola.hr.vo.HrNoticeGroupListVO;
import org.jsola.notice.vo.NoticeGroupListVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wcl
 */
@RestController("NoticeController")
@RequestMapping(HrConstants.API_PATH + "/v1/notice")
public class NoticeController implements INoticeControllerAPI {

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private SendNoticeTask sendNoticeTask;

    @Override
    @GetMapping(value = "/listAllByCompanyId")
    public Result<List<HrNoticeGroupListVO>> listAllByCompanyId(String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看通知分类权限校验

        return Result.success(
                noticeService.listAllByCompanyId(companyId, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/listAllGroup")
    public Result<List<HrNoticeGroupListVO>> listAllGroup() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看通知分类权限校验

        return Result.success(
                noticeService.listAllGroup(tokenUser)
        );
    }

    @Override
    @PostMapping("/saveOrUpdateNoticeRuleSetting")
    public Result<String> saveOrUpdateNoticeRuleSetting(@Valid @RequestBody NoticeRuleSettingDTO noticeRuleSettingDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增消息提醒规则设置权限校验
        noticeService.saveOrUpdateNoticeRuleSetting(noticeRuleSettingDTO, tokenUser);
        return Result.success("请求成功");
    }

    @Override
    @PutMapping("/open/{id}")
    public Result<Integer> openNoticeRuleSetting(@PathVariable Long id) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增消息提醒规则设置权限校验

        return Result.success(noticeService.openNoticeRuleSetting(id, tokenUser));
    }

    @Override
    @PutMapping("/close/{id}")
    public Result<Integer> closeNoticeRuleSetting(@PathVariable Long id) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增消息提醒规则设置权限校验

        return Result.success(noticeService.closeNoticeRuleSetting(id, tokenUser));
    }

    /**
     * 初始化所有用工单位的通知规则设置
     */
    @PostMapping("/initAllCompany")
    public Result<Integer> initAllCompany() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增消息提醒规则设置权限校验

        return Result.success(noticeService.initAllCompany(tokenUser));
    }

    /**
     * 处理消息提醒规则设置的接收人员userId
     */
    @PutMapping("/updateNoticeRuleSetting")
    public Result<Integer> updateNoticeRuleSetting() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增消息提醒规则设置权限校验

        return Result.success(noticeService.updateNoticeRuleSetting());
    }

    /**
     * 初始化所有用工单位的通知规则设置
     */
    @GetMapping("/sendNoticeTask")
    public Result<String> sendNoticeTask() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增消息提醒规则设置权限校验

        sendNoticeTask.contractExpireTask("");
        return Result.success("");
    }

    @Override
    @GetMapping(value = "/jfHomePageNotice")
    public Result<List<HrNoticeGroupListVO>> jfHomePageNotice() {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                noticeService.jfHomePageNotice(tokenUser)
        );
    }
}
